//---------------------------------------------------------------------------


#pragma hdrstop

#include <string.h>

#include "translateSina.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#include <stdio.h>
char null_str0[2048];
char *parseStr;

int getNextItem(char *str_i, int *offset_o, char *str_o)
{
  int offsetCount = 0;

  if (str_i[*offset_o] == 0)
  {
      str_o[offsetCount] = 0;
      return 0;
  }
  while (str_i[*offset_o] != ',' && str_i[*offset_o] != 0)
  {
    str_o[offsetCount] = str_i[*offset_o];
    offsetCount++;
    (*offset_o)++;
  }
  if (str_i[*offset_o] == ',')
    (*offset_o)++;
  str_o[offsetCount] = 0;
  
  return 1;
}

translateStock(char *str_i, StockRecord *stockRecord_o)
{
    int i = 0, numOfRecords = 0;
    char tempString[256];
    memset(stockRecord_o, 0, sizeof(*stockRecord_o));
    if  (*str_i == 0)
      return 0;
    parseStr = str_i + 4; // skip the "var "
    while (parseStr[i] != 0)
    {
      while (parseStr[i] != '\"')
          i++;
      i++;

    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // stock name
        strcpy(stockRecord_o->name, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // open
        sscanf(tempString, "%f", &stockRecord_o->open);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // last close
        sscanf(tempString, "%f", &stockRecord_o->lastClose);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // close
        sscanf(tempString, "%f", &stockRecord_o->close);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // high
        sscanf(tempString, "%f", &stockRecord_o->high);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // low
        sscanf(tempString, "%f", &stockRecord_o->low);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // buyBid
        sscanf(tempString, "%f", &stockRecord_o->buyBid);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // sellBid
        sscanf(tempString, "%f", &stockRecord_o->sellBid);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // volume
        sscanf(tempString, "%f", &stockRecord_o->volume);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // amount
        sscanf(tempString, "%f", &stockRecord_o->amount);
    }

    // {Buy1 Volume, Buy1Price}, {Buy2 Volume, Buy2Price}, {Buy5)

    for (int j=0; j<stockBid_c; j++)
    {
        if (getNextItem(parseStr, &i, tempString) == 1)
        {
            // Volume
            sscanf(tempString, "%f", &stockRecord_o->buyVolume[j]);
        }
        if (getNextItem(parseStr, &i, tempString) == 1)
        {
            // Price
            sscanf(tempString, "%f", &stockRecord_o->buyPrice[j]);
        }
    }

    // {Sell1 Volume, Sell1 Price}, {Sell2 Volume, Sell2 Price }.. {Sell5}
    for (int j=0; j<stockBid_c; j++)
    {
        if (getNextItem(parseStr, &i, tempString) == 1)
        {
            // Volume
            sscanf(tempString, "%f", &stockRecord_o->sellVolume[j]);
        }
        if (getNextItem(parseStr, &i, tempString) == 1)
        {
            // Price
            sscanf(tempString, "%f", &stockRecord_o->sellPrice[j]);
        }
    }

    // time string
    // now skip the CR/LF
    while ((parseStr[i] != '\n') && (parseStr[i] != 0))
      i++;
    if (parseStr[i] == '\n')
      i++;
    numOfRecords++;
    stockRecord_o++;
  }
  return numOfRecords;
}

translateStock(char *str_i, StockRecordString *stockRecord_o)
{
  int i = 0, numOfRecords = 0;
  char tempString[256];

  if  (*str_i == 0)
    return 0;

  parseStr = str_i + 4; // skip the "var "
  while (parseStr[i] != 0)
  {
    while (parseStr[i] != '\"')
        i++;
    i++;

    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // stock name
        strcpy(stockRecord_o->name, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // open
        strcpy(stockRecord_o->open, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // last close
        strcpy(stockRecord_o->lastClose, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // close
        strcpy(stockRecord_o->close, tempString);
    }

    // now skip the CR/LF
    while ((parseStr[i] != '\n') && (parseStr[i] != 0))
      i++;
    if (parseStr[i] == '\n')
      i++;
    numOfRecords++;
    stockRecord_o++;
  }
  return numOfRecords;

}



